package com.example.resolver;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class StudentDao {
    private final Context context;
    private final static String CONTENT_URI = "content://com.example.provider/student";

    public StudentDao(Context context) {
        this.context = context;
    }

    // 插入一条数据
    public void insert(Student student) {
        // 解析Uri
        Uri uri = Uri.parse(StudentDao.CONTENT_URI);
        // 封装数据
        ContentValues values = new ContentValues();
        values.put("name", student.getName());
        values.put("classmate", student.getClassmate());
        values.put("age", student.getAge());
        // 执行语句
        context.getContentResolver().insert(uri, values);
    }

    // 更新数据
    public void update(Student student) {
        // 解析Uri
        Uri uri = Uri.parse(StudentDao.CONTENT_URI + "/" + student.get_id());
        // 封装数据
        ContentValues values = new ContentValues();
        values.put("name", student.getName());
        values.put("classmate", student.getClassmate());
        values.put("age", student.getAge());
        // 执行语句
        context.getContentResolver().update(uri, values, null, null);
//        // 解析Uri
//        Uri uri = Uri.parse(StudentDao.CONTENT_URI);
//        // 封装数据
//        ContentValues values = new ContentValues();
//        values.put("name", student.getName());
//        values.put("classmate", student.getClassmate());
//        values.put("age", student.getAge());
//        // 执行语句
//        context.getContentResolver().update(uri, values, "_id=?",
//                new String[]{String.valueOf(student.get_id())});
    }

    // 删除一条数据
    public void delete(int _id) {
        // 解析Uri
        Uri uri = Uri.parse(StudentDao.CONTENT_URI + "/" + _id);
        // 执行语句
        context.getContentResolver().delete(uri, null, null);
//        // 解析Uri
//        Uri uri = Uri.parse(StudentDao.CONTENT_URI);
//        // 执行语句
//        context.getContentResolver().delete(uri, "_id=?",
//                new String[]{String.valueOf(_id)});
    }

    // 查询所有数据
    public List<Student> selectAll() {
        List<Student> students = new ArrayList<>();
        // 解析Uri
        Uri uri = Uri.parse(StudentDao.CONTENT_URI);
        // 查询
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        // 将查询结果转为List
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Student student = new Student(cursor.getString(
                        cursor.getColumnIndex("name")),
                        cursor.getString(cursor.getColumnIndex("classmate")),
                        cursor.getInt(cursor.getColumnIndex("age")));
                student.set_id(cursor.getInt(cursor.getColumnIndex("_id")));

                students.add(student);
            }
            // 关闭数据库
            cursor.close();
        }
         // 返回结果
        return students;
    }

    // 查询一条数据
    public Student select(int _id) {
        Student student = null;
        // 解析Uri
        Uri uri = Uri.parse(StudentDao.CONTENT_URI  + "/" + _id);
        // 查询
        Cursor cursor = context.getContentResolver().query(uri, null, null,
                null, null);
        // 获取查询结果
        assert cursor != null;
        if (cursor.moveToNext()) {
            student = new Student(cursor.getString(cursor.getColumnIndex("name")),
                    cursor.getString(cursor.getColumnIndex("classmate")),
                    cursor.getInt(cursor.getColumnIndex("age")));
        }
        // 关闭数据库
        cursor.close();
        // 返回结果
        return student;
    }
}
